package sel.me.spring.beandefinition;

import java.util.Arrays;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;


public class BeanDefinitionLearning {

	/**
	 * 持有name和aliases,为注册做准备
	 */
	BeanDefinitionHolder beanDefinitionHolder ;
	
	
	/**
	 * 配合注册时配合BeanDefinitionHolder来注册
	 */
	BeanDefinitionReaderUtils beanDefinitionReaderUtils;
	
	
	/**
	 * 提供一个接口定制化BeanDefinition since 5.0
	 */
//	BeanDefinitionCustomizer beanDefinitionCustomizer;
	
	
//	BeanDefinitionValueResolver beanDefinitionValueResolver;
	
	
	
	public void propertyReferenceTest() {
        DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();

        {
            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean2.class);
            beanFactory.registerBeanDefinition("colour",bdb.getBeanDefinition());
        }

        {
            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
            bdb.addPropertyReference("favouriteColour","colour");
            beanFactory.registerBeanDefinition("testBean",bdb.getBeanDefinition());
        }

        TestBean bean=beanFactory.getBean(TestBean.class);
    }
	
	
	
	public void beanClassWithSimpleProperty() {
		
		
		
        String[] dependsOn = new String[] { "A", "B", "C" };
        BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
        bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        bdb.addPropertyReference("age", "15");
        for (int i = 0; i < dependsOn.length; i++) {
            bdb.addDependsOn(dependsOn[i]);
        }

//        bdb.applyCustomizers(new BeanDefinitionCustomizer()
//        {
//
//            @Override
//            public void customize(BeanDefinition bd) {
//                bdb.addPropertyReference("age", "16");
//            }
//        });

        RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
        System.out.println(rbd.isSingleton());
        System.out.println(rbd.getBeanClass());
        System.out.println(Arrays.equals(dependsOn, rbd.getDependsOn()));
        System.out.println(rbd.getPropertyValues().contains("age"));
        System.out.println(rbd.getPropertyValues().get("age"));
        
    }
	
	
	public static void main(String[] args) {
		BeanDefinitionLearning beanDefinitionLearning = new BeanDefinitionLearning();
		beanDefinitionLearning.beanClassWithSimpleProperty();
	}
	
}
